package cj.web.lkg.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.constants.AppUserInfoStateEnum;
import cj.web.constants.ColumnsSubTypeEnum;
import cj.web.constants.ColumnsTypeEnum;
import cj.web.lkg.dao.AppUserDAO;
import cj.web.lkg.dao.ConfigDAO;
import cj.web.lkg.dao.NewsDAO;
import cj.web.login.controllers.BaseController;

@Path("/devs")
public class DevicesController extends BaseController{
	private static final String IOS = "IOS";
	private static final String AZ = "AZ";
	private static final String gywmCode = "GYWM";
	
	private static Logger logger = Logger.getLogger(DevicesController.class.getName());

	@Autowired
	private AppUserDAO appuserDao;
	@Autowired
	private ConfigDAO configDao;
	@Autowired
	private NewsDAO newsDao;
	
	/**
	 * app安装完成后向后台推送设备信息
	 * @param devicesId
	 * @return
	 * @throws Exception 
	 */
	@Get("createUc")
	public String createUc(@Param("devicesId") String devicesId,@Param("appclient") String appclient) throws Exception {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> reps = appuserDao.queryAppUserByDevicesId(devicesId);
		
		if(reps.size()==0)
		{
			Map rep = new HashMap();
			rep.put("appclient", appclient);
			rep.put("devicesId", devicesId);
			rep.put("state", AppUserInfoStateEnum.ZC.getValue());
			rep.put("deleteFlag", Constants.deleteFlag.deleteFlag_1);
			rep.put("createUser", devicesId);
			rep.put("updateUser", devicesId);
			appuserDao.create(rep);
		}
		else
		{
			if(appclient!=null && !"".equals(appclient))
			{
				appuserDao.updateAppUserInfo(devicesId, appclient);
			}
		}
		
		//判断是否存在访问记录
		int tjSize = configDao.queryTJSizeById(devicesId);
		
		if(tjSize==0)
			//不存在添加访问记录
		{
			configDao.createUserTJ(devicesId);
		}
		
		List<Map> ptlmReps = configDao.queryColumnsPtlmAll();
		List<Map> actions = configDao.queryActionAdvertisement();
		for(Map obj:actions)
		{
			obj.put("carouselImage", imgPath+obj.get("carouselImage").toString());
			obj.put("releaseAt", DateUtils.getMinusTime(obj.get("releaseAt").toString(), DateUtils.DATE_FULL_STR, 0));
		}
		jo.put("actions", actions);
		jo.put("ptlmReps", ptlmReps);
		jo.put("lmlis", returnPtlmLisMap());
//		jo = getList(jo,Integer.parseInt(ptlmReps.get(0).get("id").toString()),0,10);
		jo.put("flagCode", "0");//启动登录模式：0、开；1、关
		return "@"+jo.toString();	
	}
	
	/**
	 * 获取新闻列表By	ColumnsId
	 * @param jo
	 * @param columnsId
	 * @param start
	 * @param length
	 * @return
	 * @throws Exception
	 */
	private JSONObject getList(JSONObject jo,int columnsId, int start, int length) throws Exception{
		NewsController newsController = new NewsController();
		
		List<Map> tops = newsDao.queryTopByColumnsId(columnsId);//获取头条
		tops = newsController.formReturnList(tops);//时间格式化，图集新闻图片提取，外链新闻，形成跳转url
		jo.put("tops", tops);
		
		List<Integer> ids = new ArrayList<Integer>();//排除头条，请求列表
		for(Map map : tops){
			ids.add(Integer.valueOf(map.get("id").toString()));
		}
		
		List<Map> news = newsDao.queryNewsByColumnsId(ids, columnsId, start, length);//新闻列表
		news = newsController.formReturnList(news);//时间格式化，图集新闻图片提取，外链新闻，形成跳转url
		jo.put("list", news);
		
		int size = newsDao.querySizeByColumnsId(ids, columnsId);
		jo.put("total", size);
		
		return jo;
	}
	
	
	
	/**
	 * 栏目列表
	 * @return
	 */
	@Get("applm")
	public String applm() {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> reps = configDao.queryColumnsAll();
		
		List<Map> ptlms = new ArrayList<Map>();//普通
		List<Map> jdlms = new ArrayList<Map>();//街道
		List<Map> yqlms = new ArrayList<Map>();//园区
		
		for(Map rep:reps)
		{
			Map ptlm = new HashMap();//普通
			Map jdlm = new HashMap();//街道
			Map yqlm = new HashMap();//园区
			if(ColumnsTypeEnum.LM.getValue()==Integer.parseInt(rep.get("type").toString()))
			{
				//普通
				if(ColumnsSubTypeEnum.PTLM.getValue()==Integer.parseInt(rep.get("subType").toString()))
				{
					ptlm.put("id", rep.get("id").toString());
					ptlm.put("name", rep.get("name").toString());
					ptlm.put("descs", rep.get("descs").toString());
					
					ptlms.add(ptlm);
				}
				//街道
				if(ColumnsSubTypeEnum.JDLM.getValue()==Integer.parseInt(rep.get("subType").toString()))
				{
					jdlm.put("id", rep.get("id").toString());
					jdlm.put("name", rep.get("name").toString());
					jdlm.put("descs", rep.get("descs").toString());
					
					jdlms.add(jdlm);
				}
				//园区
				if(ColumnsSubTypeEnum.YQLM.getValue()==Integer.parseInt(rep.get("subType").toString()))
				{
					yqlm.put("id", rep.get("id").toString());
					yqlm.put("name", rep.get("name").toString());
					yqlm.put("descs", rep.get("descs").toString());
					
					yqlms.add(yqlm);
				}
			}
			
		}
		
		List<Map> objls =new ArrayList<Map>();
		
		Map replm1 = new HashMap();
		replm1.put("name", ColumnsSubTypeEnum.PTLM.getName());
		replm1.put("value", ColumnsSubTypeEnum.PTLM.getValue());
		replm1.put("data", ptlms);
		objls.add(replm1);
		Map replm2 = new HashMap();
		replm2.put("name", ColumnsSubTypeEnum.YQLM.getName());
		replm2.put("value", ColumnsSubTypeEnum.YQLM.getValue());
		replm2.put("data", yqlms);
		objls.add(replm2);
		Map replm3 = new HashMap();
		replm3.put("name", ColumnsSubTypeEnum.JDLM.getName());
		replm3.put("value", ColumnsSubTypeEnum.JDLM.getValue());
		replm3.put("data", jdlms);
		objls.add(replm3);
		
		jo.put("ls", objls);
		
		return "@"+jo.toString();	
	}
	
	/**
	 * 获取所有普通栏目LisMap
	 * @return
	 */
	private List<Map> returnPtlmLisMap()
	{
		List<Map> reps = configDao.queryColumnsAll();
		
		List<Map> ptlms = new ArrayList<Map>();//普通
		List<Map> jdlms = new ArrayList<Map>();//街道
		List<Map> yqlms = new ArrayList<Map>();//园区
		
		for(Map rep:reps)
		{
			Map ptlm = new HashMap();//普通
			Map jdlm = new HashMap();//街道
			Map yqlm = new HashMap();//园区
			if(ColumnsTypeEnum.LM.getValue()==Integer.parseInt(rep.get("type").toString()))
			{
				//普通
				if(ColumnsSubTypeEnum.PTLM.getValue()==Integer.parseInt(rep.get("subType").toString()))
				{
					ptlm.put("id", rep.get("id").toString());
					ptlm.put("name", rep.get("name").toString());
					
					ptlms.add(ptlm);
				}
				//街道
				if(ColumnsSubTypeEnum.JDLM.getValue()==Integer.parseInt(rep.get("subType").toString()))
				{
					jdlm.put("id", rep.get("id").toString());
					jdlm.put("name", rep.get("name").toString());
					
					jdlms.add(jdlm);
				}
				//园区
				if(ColumnsSubTypeEnum.YQLM.getValue()==Integer.parseInt(rep.get("subType").toString()))
				{
					yqlm.put("id", rep.get("id").toString());
					yqlm.put("name", rep.get("name").toString());
					
					yqlms.add(yqlm);
				}
			}
			
		}
		
		List<Map> objls =new ArrayList<Map>();
		
		Map replm1 = new HashMap();
		replm1.put("name", ColumnsSubTypeEnum.PTLM.getName());
		replm1.put("value", ColumnsSubTypeEnum.PTLM.getValue());
		replm1.put("data", ptlms);
		objls.add(replm1);
//		Map replm2 = new HashMap();
//		replm2.put("name", ColumnsSubTypeEnum.YQLM.getName());
//		replm2.put("value", ColumnsSubTypeEnum.YQLM.getValue());
//		replm2.put("data", yqlms);
//		objls.add(replm2);
		Map replm3 = new HashMap();
		replm3.put("name", ColumnsSubTypeEnum.JDLM.getName());
		replm3.put("value", ColumnsSubTypeEnum.JDLM.getValue());
		replm3.put("data", jdlms);
		objls.add(replm3);
		
		return objls;
	}
	
	/**
	 * App版本
	 * @return
	 */
	@Get("appVersion")
	public String appVersion() {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> reps = configDao.queryById(IOS);
		Double version =Double.valueOf(reps.get(0).get("value").toString());
		jo.put("version", version);
		jo.put("download", reps.get(0).get("descs").toString());
		return "@"+jo.toString();	
	}
	
	/**
	 * App版本
	 * @return
	 */
	@Get("appVersionAZ")
	public String appVersionAZ() {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> reps = configDao.queryById(AZ);
		Double version =Double.valueOf(reps.get(0).get("value").toString());
		jo.put("version", version);
		jo.put("download", reps.get(0).get("descs").toString());
		return "@"+jo.toString();	
	}
	
	/**
	 * 关于我们
	 * @return
	 */
	@Get("appGYWM")
	public String appGYWM() {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> reps = configDao.queryById(gywmCode);
		jo.put("name", reps.get(0).get("name"));
		jo.put("value", reps.get(0).get("value"));
		jo.put("updateAt", DateUtils.getMinusTime(reps.get(0).get("updateAt").toString(), DateUtils.DATE_FULL_STR, 0));
		return "@"+jo.toString();	
	}
}
